package values.quest
{
import flash.events.EventDispatcher;

import mx.utils.UIDUtil;

import values.events.QuestValueEvent;

[Event(name="appear", type="values.events.QuestValueEvent")]
[Event(name="accept", type="values.events.QuestValueEvent")]
[Event(name="viewDetail", type="values.events.QuestValueEvent")]
[Event(name="progress", type="values.events.QuestValueEvent")]
[Event(name="complete", type="values.events.QuestValueEvent")]

public class QuestValue extends EventDispatcher
{
	private var _uid:String = UIDUtil.createUID();
	
	public function get uid():String
	{
		return _uid;
	}
	
	private var _template:QuestTemplateValue;
	
	public function get template():QuestTemplateValue
	{
		return _template;
	}
	
	public function set template(value:QuestTemplateValue):void
	{
		_template = value;
		
		for each (var goalTemplate:QuestGoalTemplateValue in template.goals)
		{
			var goal:QuestGoalValue = new QuestGoalValue();
			goal.template = goalTemplate;
			goal.owner = this;
			goals.push(goal);
		}
	}
	
	private var _goals:Vector.<QuestGoalValue> = new Vector.<QuestGoalValue>();

	public function get goals():Vector.<QuestGoalValue>
	{
		return _goals;
	}
	
	private var _isAccepted:Boolean = false;

	public function get isAccepted():Boolean
	{
		return _isAccepted;
	}
	
	public function get isCompleted():Boolean
	{
		for each (var goal:QuestGoalValue in goals)
		{
			if (!goal.isCompleted) return false;
		}
		return true;
	}
	
	public function appear():void
	{
		dispatchEvent(new QuestValueEvent(QuestValueEvent.APPEAR, this));
	}
	
	public function accept():void
	{		
		_isAccepted = true;
		
		dispatchEvent(new QuestValueEvent(QuestValueEvent.ACCEPT, this));
	}
	
	public function viewDetail():void
	{
		dispatchEvent(new QuestValueEvent(QuestValueEvent.VIEW_DETAIL, this));
	}
	
	public function update():void
	{
		dispatchEvent(new QuestValueEvent(QuestValueEvent.PROGRESS, this));
	}
	
	public function complete():void
	{		
		dispatchEvent(new QuestValueEvent(QuestValueEvent.COMPLETE, this));
	}

}
}